package com.ruoyi.controller.jpjs.expensereport;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.service.jpjs.PublicService;
import com.ruoyi.service.jpjs.expensereport.ExpenseReportService;
import com.ruoyi.util.BaseResult;
import com.ruoyi.util.ResultEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Map;




/**
 * @author zeo
 * 费用报表 */

@Controller
@RequestMapping("/expenseReport")
public class ExpenseReportController extends BaseController {

    @Autowired
    ExpenseReportService expenseReportService;

    @Autowired
    PublicService publicService;

    private String prefix = "jpjs/expensereport";

    @GetMapping()
    public String grossProfit(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserCompAndJszb());
        return prefix + "/expenseReport";
    }

    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(@RequestParam Map map) {
        startPage();
        return getDataTable(expenseReportService.list(map));
    }

    @GetMapping("/add")
    public String add(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/add";
    }

    @RequestMapping("/add")
    @ResponseBody
    public AjaxResult add(@RequestParam Map map) {
        return toAjax(expenseReportService.add(map));
    }

    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        mmap.put("obj", expenseReportService.findExpenseReportById(id));
        return prefix + "/edit";
    }

    @RequestMapping("/edit")
    @ResponseBody
    public AjaxResult edit(@RequestParam Map map) {
        return toAjax(expenseReportService.edit(map));
    }



    //开始编写填写某年某月费用情况


    /**
     * 跳转初始化页面
     *
     * @return
     */
    @GetMapping("/expenseReportYearMonth")
    public String expenseReportYearMonth(ModelMap mmap, String type, String id) {
        if (type != null && id != null) {
            mmap.put("type", type);
            mmap.put("id", id);
        }
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/expenseReportYearMonth";
    }

    /**
     * 获取当前用户当前权限下所有项目费用数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/findProList")
    @ResponseBody
    public TableDataInfo findProList(@RequestParam Map map) {
        Object type = map.get("type");
        Object id = map.get("id");
        if (type != null && id != null) {
            Map map1 = expenseReportService.findExpenseReportById(id.toString());
            String compId = map1.get("compId").toString();
            String depOrProName = map1.get("depOrProName").toString();
            map.put("compId",compId);
            if ("2".equals(type)) {
                //如果type=2代表检索工程公司和项目名称，因此将项目名称存入map中即可
                map.put("depOrProName",depOrProName);
            }
        }

        startPage();
        return getDataTable(expenseReportService.findProList(map));
    }

    /**
     * 跳转新增需要填写项目费用的页面
     */
    @GetMapping("/addProValue")
    public String addProValue(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/addProValue";
    }


    /**
     * 根据工程公司id筛选出对应的项目集合
     */
    @RequestMapping("/findProByCompId")
    @ResponseBody
    public BaseResult findProByCompId(String compId,String valueType) {
        return BaseResult.build(ResultEnum.SUCCESS, expenseReportService.findProByCompId(compId,valueType));
    }

    /**
     * 新增一条年项目费用数据
     */
    @RequestMapping("/addProValue")
    @ResponseBody
    public AjaxResult addProValue(@RequestParam Map map) {
        return toAjax(expenseReportService.addProValue(map));
    }

    /**
     * 跳转填写某年某月的项目费用页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editProValue")
    public String editProValue(String id, ModelMap mmap) {
        mmap.put("obj", expenseReportService.findMonthExpenseReportById(id));
        return prefix + "/editProValue";
    }

    /**
     * 填写某年某月的计划产值
     */
    @RequestMapping("/editProValue")
    @ResponseBody
    public AjaxResult editProValue(@RequestParam Map map) {
        return toAjax(expenseReportService.editProValue(map));
    }

    /**
     * 判断年是否填过
     */
    @RequestMapping("/findYearProCount")
    @ResponseBody
    public BaseResult findYearProCount(@RequestParam Map map) {
        int count = expenseReportService.findYearProCount(map);
        if (count > 0) {
            return BaseResult.build(ResultEnum.YEAR_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }

    /**
     * 判断某年某月是否填过
     */
    @RequestMapping("/findYearProMonthCount")
    @ResponseBody
    public BaseResult findYearProMonthCount(@RequestParam Map map) {
        String value = expenseReportService.findYearProMonthCount(map);
        if (value != null) {
            return BaseResult.build(ResultEnum.YEAR_MONTH_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }




    /**
     * 获取当前用户当前权限下所有职能部门费用数据
     *
     * @param map
     * @return
     */
    @RequestMapping("/findDepList")
    @ResponseBody
    public TableDataInfo findDepList(@RequestParam Map map) {
        Object type = map.get("type");
        Object id = map.get("id");
        if (type != null && id != null) {
            Map map1 = expenseReportService.findExpenseReportById(id.toString());
            String compId = map1.get("compId").toString();
            String depOrProName = map1.get("depOrProName").toString();
            map.put("compId",compId);
            if ("2".equals(type)) {
                //如果type=2代表检索工程公司和项目名称，因此将项目名称存入map中即可
                map.put("depOrProName",depOrProName);
            }
        }
        startPage();
        return getDataTable(expenseReportService.findDepList(map));
    }


    /**
     * 跳转新增需要填写职能部门费用的页面
     */
    @GetMapping("/addDepValue")
    public String addDepValue(ModelMap mmap) {
        mmap.put("userComp", publicService.findUserComp());
        return prefix + "/addDepValue";
    }


    /**
     * 新增一条年职能部门费用数据
     */
    @RequestMapping("/addDepValue")
    @ResponseBody
    public AjaxResult addDepValue(@RequestParam Map map) {
        return toAjax(expenseReportService.addDepValue(map));
    }

    /**
     * 跳转填写某年某月的职能部门费用页面
     *
     * @param id
     * @param mmap
     * @return
     */
    @GetMapping("/editDepValue")
    public String editDepValue(String id, ModelMap mmap) {
        mmap.put("obj", expenseReportService.findMonthExpenseReportById(id));
        return prefix + "/editDepValue";
    }

    /**
     * 填写某年某月的计划产值
     */
    @RequestMapping("/editDepValue")
    @ResponseBody
    public AjaxResult editDepValue(@RequestParam Map map) {
        return toAjax(expenseReportService.editDepValue(map));
    }

    /**
     * 判断年是否填过
     */
    @RequestMapping("/findYearDepCount")
    @ResponseBody
    public BaseResult findYearDepCount(@RequestParam Map map) {
        int count = expenseReportService.findYearDepCount(map);
        if (count > 0) {
            return BaseResult.build(ResultEnum.YEAR_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }

    /**
     * 判断某年某月是否填过
     */
    @RequestMapping("/findYearDepMonthCount")
    @ResponseBody
    public BaseResult findYearDepMonthCount(@RequestParam Map map) {
        String value = expenseReportService.findYearDepMonthCount(map);
        if (value != null) {
            return BaseResult.build(ResultEnum.YEAR_MONTH_PLAN_TO_FILL);
        } else {
            return BaseResult.build(ResultEnum.SUCCESS);
        }
    }
}

